﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.EntityType;
using Sector7.Medical.Events;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// RecipeItemControl.xaml 的交互逻辑
    /// </summary>
    public partial class RecipeItemControl : AddModeControlBase
    {
        #region Accessor
        /// <summary>
        /// 编辑完成事件
        /// </summary>
        public event EditCompletedEventHandler EditCompleted = null;

        /// <summary>
        /// 获取或设置当前处方项
        /// </summary>
        public M7_RecipeItem RecipeItem
        {
            get { return m_entity as M7_RecipeItem; }
            set { m_entity = value; }
        }

        /// <summary>
        /// 获取或设置当前处方项序号
        /// </summary>
        public int No
        {
            get { return m_no; }
            set
            {
                if (value != m_no)
                {
                    m_no = value;

                    lblNo.Content = m_no.ToString();
                }
            }
        }
        #endregion

        #region Public functions
        public M7_RecipeItem GetRecipeItem()
        {
            if (RecipeItem == null)
            {
                RecipeItem = new M7_RecipeItem();
            }

            if (CheckData())
            {
                RecipeItem = new M7_RecipeItem();
                RecipeItem.DrugId = Convert.ToInt32(cbbDrugList.SelectedValue);
                RecipeItem.SpecId = ((cbbSpec.SelectedItem as ComboBoxItem).Tag as M7_DrugSpec).SpecId;
                RecipeItem.Number = Convert.ToDecimal(txbNumber.Text);
                RecipeItem.UsageNumber = Convert.ToDecimal(txbUsageNumber.Text);                
                RecipeItem.FrequencyId = cbbFreqList.SelectedValue.ToString();
                RecipeItem.ApproachId = cbbApprList.SelectedValue.ToString();
            }
            else
            {
                ShowErrors();
            }

            return RecipeItem;
        }

        public void SetRecipeItem(M7_RecipeItem item)
        {
            m_drugList = Agent_INFS.GetDrugListByKeywords(item.DrugId.ToString());
            ClientUtil.LoadItemsSource(cbbDrugList, m_drugList);
            cbbDrugList.SelectedValue = item.DrugId;

            txbNumber.Text = item.Number.ToString();
            txbUsageNumber.Text = item.UsageNumber.ToString();

            cbbFreqList.SelectedValue = item.FrequencyId;
            cbbApprList.SelectedValue = item.ApproachId;
        }
        #endregion

        #region Private functions
        protected new void GetData()
        {
            m_freqList = Agent_INFS.GetRecipeFrequencyList();
            ClientUtil.LoadItemsSource(cbbFreqList, m_freqList);
            cbbFreqList.SelectedIndex = -1;

            m_apprList = Agent_INFS.GetRecipeApproachList();
            ClientUtil.LoadItemsSource(cbbApprList, m_apprList);
            cbbApprList.SelectedIndex = -1;

            base.GetData();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();
            // 检查合法性
            string number = txbNumber.Text;
            string usageNumber = txbUsageNumber.Text;

            if (string.IsNullOrEmpty(number))
            {
                m_errors.Add("包装单位", "不能为空!");
            }

            if (string.IsNullOrEmpty(usageNumber))
            {
                m_errors.Add("使用单位", "不能为空!");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            // 整合数据
            /*
            RecipeItem = new M7_RecipeItem();
            RecipeItem.DrugId = Convert.ToInt32(cbbDrugList.SelectedValue);
            RecipeItem.Number = Convert.ToDecimal(txbNumber.Text);
            RecipeItem.UsageNumber = Convert.ToDecimal(txbUsageNumber.Text);
            RecipeItem.FrequencyId = cbbFreqList.SelectedValue.ToString();
            RecipeItem.ApproachId = cbbApprList.SelectedValue.ToString();
             * */

            // 触发事件
            base.AddData();
            OnEditCompleted(new EditCompletedEventArgs(CrudType.Create));
        }

        protected void OnEditCompleted(EditCompletedEventArgs e)
        {
            if (EditCompleted != null)
            {
                EditCompleted(this, e);
            }
        }

        private void DrugComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
            if (selectedDrug != null)
            {
                cbbSpec.Items.Clear();
                foreach(var drugSpec in selectedDrug.SpecList)
                {
                    // 规格 规格单位 × 包装规格 使用单位 / 包装单位
                    var item = new ComboBoxItem();
                    item.Content = string.Format("{0} {1} × {2} {3} / {4}",
                        selectedDrug.Spec.ToString().TrimEnd('0'),
                        selectedDrug.SpecUnit.UnitName,
                        drugSpec.PackSpec.ToString().TrimEnd('0'),
                        selectedDrug.UsageUnit.UnitName,
                        drugSpec.PackUnit.UnitName);
                    item.Tag = drugSpec;

                    cbbSpec.Items.Add(item);
                }

                cbbSpec.SelectedIndex = 0;

                if (cbbSpec.SelectedItem != null)
                {
                    var selectedSpec = (cbbSpec.SelectedItem as ComboBoxItem).Tag as M7_DrugSpec;
                    lblPackUnit.Content = selectedSpec.PackUnit.UnitName;
                    this.Tag = selectedSpec.Price;
                }
                lblUsageUnit.Content = selectedDrug.UsageUnit.UnitName;
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            DoAddAndExit();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除当前处方项？");
            if (result == MessageBoxResult.Yes)
            {
                OnEditCompleted(new EditCompletedEventArgs(CrudType.Delete));
            }
        }

        private void AddModeControlBase_Loaded(object sender, RoutedEventArgs e)
        {
            //RecipeItem = new M7_RecipeItem();
            //this.DataContext = m_entity;

            GetData();
        }

        private void cbbSpec_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbbSpec.SelectedItem != null)
            {
                lblPackUnit.Content = ((cbbSpec.SelectedItem as ComboBoxItem).Tag as M7_DrugSpec).PackUnit.UnitName;
            }
        }
        #endregion

        #region Structure
        public RecipeItemControl()
        {
            InitializeComponent();

            GetData();
        }

        public RecipeItemControl(int no)
        {
            InitializeComponent();

            No = no;

            GetData();
        }
        #endregion

        #region Inner members
        private int m_no = -1;

        private List<M7_RecipeItem> m_recipeItemList = null;

        private List<M7_Drug> m_drugList = null;

        private List<M7_ApproachForRecipe> m_apprList = null;
            
        private List<M7_FrequencyForRecipe> m_freqList = null;
        #endregion
    }
}
 